package cn.ccenjie.wsy.utils;

import cn.ccenjie.wsy.entity.User;
import cn.ccenjie.wsy.vo.Resp;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.util.StringUtils;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Base64;
import java.util.Map;

public class HttpUtils {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();


    /**
     * 获取参数
     */
    public static Object getParam(String key, HttpServletRequest request) {
        String contentType = request.getHeader("Content-Type");
        if("application/json".equals(contentType)) {
            try {
                ServletInputStream inputStream = request.getInputStream();
                byte[] buffer = new byte[1024];
                int i;
                StringBuilder sb = new StringBuilder();
                while ((i = inputStream.read(buffer)) != -1) {
                    sb.append(new String(buffer, 0, i));
                }

                if(!StringUtils.isEmpty(sb.toString())) {
                    Map map = OBJECT_MAPPER.readValue(sb.toString(), Map.class);
                    return map.get(key);
                }
            }catch (Exception e) {
                throw new RuntimeException(e);
            }

        }else {
            return request.getParameter(key);
        }
        return null;
    }


    /**
     * 响应信息
     */
    private static void wrire(HttpServletResponse response, String str, String contentType) {
        response.setContentType(contentType);
        response.setCharacterEncoding("UTF-8");
        try {
            PrintWriter writer = response.getWriter();
            writer.print(str);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 响应JSON信息
     */
    public static void wrireJson(HttpServletResponse response, String str) {
        wrire(response, str, "application/json");
    }

    /**
     * 响应JSON信息
     */
    public static void wrireJson(HttpServletResponse response, Map str) {
        try {
            String xx = OBJECT_MAPPER.writeValueAsString(str);
            wrire(response, xx, "application/json");
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }


    public static void wrire(HttpServletResponse response, Resp resp) {
        try {
            String xx = OBJECT_MAPPER.writeValueAsString(resp);
            wrire(response, xx, "application/json");
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 响应空白文件
     */
    public static void wrireText(HttpServletResponse response, String str) {
        wrire(response, str, "text/plain");
    }

    /**
     * 获取头部信息
     */
    public static String getHader(String key, HttpServletRequest request) {
        return request.getHeader(key);
    }

    public static String secret(String token) {
        String[] arr = token.split(":");
        return arr[1];
    }

    public static User userInfo(String token) {
        String[] arr = token.split(":");
        String data = new String(Base64.getDecoder().decode(arr[0]));
        try {
            return OBJECT_MAPPER.readValue(data, User.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
